<?php
/**
 * CreateTime: 2022/4/30 14:18
 */

namespace App\Models\Dictionary;

use App\Consts\BaseConsts\BaseConsts;
use App\Consts\TableConsts\TableConsts;
use App\Models\BaseModel;
use App\Models\BaseModelV2;
use App\Scopes\MediationCompanyAndUpComScope;
use App\Scopes\MediationCompanyIdScope;

class DictionaryModel extends BaseModelV2
{
    protected $table = TableConsts::CY_DATA_DICTIONARY_TABLE_NAME;

    protected $primaryKey = TableConsts::CY_DATA_DICTIONARY_TABLE_PRIMARY_KEY;

    protected $fillable = [
        'dictionary_id',
        'create_at',
        'parent_id',
        'dictionary_name',
        'dictionary_value',
        'dictionary_key',
    ];

    public $timestamps = false;

    protected static function booted()
    {
        static::addGlobalScope(new MediationCompanyIdScope());
    }

    public function addDictionary($dictionaryData){
        return $this->insertGetId($dictionaryData);
    }

    public function editDictionaryByDictionaryId($dictionaryData){
        return $this
            ->where("dictionary_id",$dictionaryData['dictionary_id'])
            ->update($dictionaryData);
    }

    /**
     * 查询下一级字典子节点
     * @param $dictionaryIds
     * @return mixed
     */
    public function getDictionaryNextByDictionaryIds($dictionaryIds,$select=["*"]): mixed
    {
        return $this
            ->select($select)
            ->where(function ($query) use ($dictionaryIds){
               $query->whereIn('dictionary_id',$dictionaryIds);
               $query->orWhereIn('parent_id',$dictionaryIds);
            })->where('status',BaseConsts::STATUS_NORMAL)->get();
    }

    public function getDictionaryByDictionaryList(){

    }

    public function getDictionaryAllDictionary()
    {
        return $this
            ->select([
                'dictionary_id',
                'parent_id',
                'dictionary_name as title',
                'dictionary_value as value',
                'dictionary_key as childKey',
                'is_disable'
            ])
//            ->where(function ($query){
//                $query->where('parent_id',100);
//                $query->orWhere('dictionary_id',100);
//            })
            ->where('status',BaseConsts::STATUS_NORMAL)
            ->orderBy('value')
            ->get();
    }


    public function getDictionaryInfoByDictionaryId($dictionaryId, $select = ["*"])
    {
        return $this
            ->select($select)
            ->where('dictionary_id',$dictionaryId)
            ->where('status',BaseConsts::STATUS_NORMAL)
            ->first();
    }

    public function getDictionaryInfoByDictionaryKey($dictionaryKey, $select = ["*"]){
        return $this
            ->select($select)
            ->where('dictionary_key',$dictionaryKey)
            ->where('status',BaseConsts::STATUS_NORMAL)
            ->first();
    }


    public function getDictionaryNodeByDictionaryId($dictionaryId, $select = ["*"]){
        return $this
            ->select($select)
            ->where('parent_id',$dictionaryId)
            ->where('status',BaseConsts::STATUS_NORMAL)
            ->get();
    }


    // 数据处理
    public function getIsDisableAttribute($value): bool
    {
        return $value;
    }

	/* get all sub node data by parent key from dictionary
	 *
	 */
	public static function getAllSubNodeDataByParentkey($parent_key){
		
		app()->gcHelper->gfDbQuery()->select("	
								
			select 
					* 
			from 
					".app()->gcHelper->gfGetTableNameByConstKey('CY_DATA_DICTIONARY_TABLE_NAME')." 
			where 
					parent_id in (select dictionary_id from ".app()->gcHelper->gfGetTableNameByConstKey('CY_DATA_DICTIONARY_TABLE_NAME')." where dictionary_key=?)
			order by 
					dictionary_id desc
		"
		,[$parent_key]
		)->forEachRow(function ($row){
			
			return $row->getOneRowAllData();
		},$ar);

		#
		#
		return $ar['content'];
	}

    public function deleteDictionary($deleteDictionaryId)
    {
        return $this
            ->where('dictionary_id',$deleteDictionaryId)
            ->delete();
    }
}